//#include <QCoreApplication>
#include <QMap>
#include <QDebug>
#include <QVector>

#include <iostream>
#include <cstring>
#include <vector>
#include "ClString.h"

using namespace std;


class MeasurementInterface{
public:
    struct MeasureInfo {
        float real_distance;
        float pixel_distance;
    };
};

int main(int argc, char *argv[])
{
//    QCoreApplication a(argc, argv);
//    "FAI25-D-position", "FAI25-D-X-distance_real", "FAI25-D-Y-distance_real");
    string Fai25_x_real   = "FAI25-D-X-distance_real";
    string Fai25_y_real   = "FAI25-D-Y-distance_real";
    string Fai25_position = "FAI25-D-position";

    vector<std::string> itemList{"FAI25-D-X-distance_real",
                                 "FAI25-D-Y-distance_real",
                                 "FAI25-D-position"
                                };
    for(auto& item: itemList) {
        ClString clString;
        std::string result = clString.SplitStr(item, "-position");
        std::string itemName = clString.SplitStr(result, "-");
        if(result.find(item) > 0) {
            cout << "result : " << result << endl;
            cout << "item: "    << itemName   <<  endl;
        }
        cout << itemName << endl;
    }

    QMap<QString, MeasurementInterface::MeasureInfo> qMapInfo;

    MeasurementInterface::MeasureInfo measureInfo;
    measureInfo.pixel_distance = 1;
    measureInfo.real_distance  = 1;

    qMapInfo["FAI25-D-X-distance"] = measureInfo;
    qMapInfo["FAI25-D-Y-distance"] = measureInfo;
    qMapInfo["FAI25-D-position"] = measureInfo;

    qMapInfo["FAI19-D-X-distance"] = measureInfo;
    qMapInfo["FAI19-D-Y-distance"] = measureInfo;
    qMapInfo["FAI19-D-position"] = measureInfo;

    qMapInfo["FAI18-D-X-distance"] = measureInfo;
    qMapInfo["FAI20-D-X-distance"] = measureInfo;

    struct PosRelatedXYValue {
        double xCorrectDis;
        double yCorrectDis;
    };

    QMap<QString, PosRelatedXYValue> ItemMapLists;
    QStringList itemLists;

    QMap<QString, MeasurementInterface::MeasureInfo>::ConstIterator iter = qMapInfo.constBegin();
    for(iter; iter != qMapInfo.constEnd(); iter++) {
        QString keyStr = iter.key();
        if(keyStr.contains("-position")) {
            int startIndex = keyStr.indexOf("-");
            if(-1 != startIndex) {
                QString item = keyStr.mid(0, startIndex);
                itemLists.append(item);
            }
        }
    }

    for(auto& item: itemLists) {
        for(iter = qMapInfo.constBegin(); iter != qMapInfo.constEnd(); iter++) {
            QString keyStr = iter.key();
            qDebug() << "key str: " << keyStr;

            if(keyStr.contains(item)) {
                if(keyStr.contains("-X-")) {
                    qDebug() << "X: " << iter.value().real_distance;
                    ItemMapLists[item].xCorrectDis = iter.value().real_distance;
                }
                if(keyStr.contains("-Y-")) {
                    qDebug() << "Y: " << iter.value().real_distance;
                    ItemMapLists[item].yCorrectDis = iter.value().real_distance;
                }
            }
        }
    }

    // calculate positon
    for(auto& item: itemLists) {
        for(iter = qMapInfo.constBegin(); iter != qMapInfo.constEnd(); iter++) {
            QString mapKeyStr = iter.key();
            if(mapKeyStr.contains(item) && (mapKeyStr.contains("-position"))) {
                qDebug() << "position: " << mapKeyStr;
                double pos = 2*sqrt((ItemMapLists[item].xCorrectDis - 0.7)
                                    + (ItemMapLists[item].xCorrectDis - 1));
                qMapInfo[mapKeyStr].real_distance = pos;
            }
        }
    }

    int temp = 0;
//    return a.exec();
}
